﻿using System;
using System.Windows;
using Rio.Framework.Pages.Events;
using Rio.Framework.Pages.AppCode.Enums;
using Rio.Framework.Pages.Context;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Pages.Controls.BaseClass.Navigation;
using Rio.Framework.Localisation;
using Telerik.Windows.Controls;

namespace Rio.Framework.Pages.Controls
{
    /// <summary>
    /// This class manage the state of all items available in context menu
    /// </summary>
    public partial class DocumentContextMenu : NavigationBase
    {
        #region Events
        public delegate void DocumentContextMenuEventHandler(object sender, DocumentRibbonBarEventArgs args);
        public event DocumentContextMenuEventHandler DocumentContextMenuEvent;
        #endregion

        #region Constructors
        public DocumentContextMenu()
        {
            InitializeComponent();
            InitLabel();
        }
        #endregion

        #region Public
        /// <summary>
        /// Initialize all framework element labels
        /// </summary>
        public override sealed void InitLabel()
        {
            Delete.Header = Resx.GetLabel(ResxCommon.DELETE);
            ZoomIn.Header = Resx.GetLabel(ResxCommon.ZOOMIN);
            ZoomOut.Header = Resx.GetLabel(ResxCommon.ZOOMOUT);
            RotateLeft.Header = Resx.GetLabel(ResxCommon.ROTATELEFT);
            RotateRight.Header = Resx.GetLabel(ResxCommon.ROTATERIGHT);
            SaveModifications.Header = Resx.GetLabel(ResxCommon.SAVE_MODIFICATION);
            MergePDF.Header = Resx.GetLabel(ResxCommon.MERGE_PDF);

            ExplodePDF.Header = Resx.GetLabel(ResxCommon.EXPLODE_PDF);

            DocumentMode.Header = Resx.GetLabel(ResxCommon.DISPLAYVIEWERMODE);

            ThumbnailsMode.Header = Resx.GetLabel(ResxCommon.DISPLAYTHUMBNAILMODE);
            ListMode.Header = Resx.GetLabel(ResxCommon.DISPLAYLISTMODE);

            Download.Header = Resx.GetLabel(ResxCommon.SAVE_DOCUMENT);
            Print.Header = Resx.GetLabel(ResxCommon.PRINT);
            SendEmail.Header = Resx.GetLabel(ResxCommon.SEND_BY_MAIL);
            Ventilate.Header = Resx.GetLabel(ResxCommon.VENTIL);
            Split2.Header = Resx.GetLabel(ResxCommon.SPLIT_IMAGE_X2);
            Split3.Header = Resx.GetLabel(ResxCommon.SPLIT_IMAGE_X3);
            OpenDirectory.Header = Resx.GetLabel(ResxListVignetteSearch.OPEN_DIRECTORY);
            AddVersion.Header = Resx.GetLabel(ResxFolderPage.ADD_NEW_VERSION);
            MakeToMajorVersion.Header = Resx.GetLabel(ResxFolderPage.MAKE_MAJOR_VERSION);
            Cut.Header = Resx.GetLabel(ResxCommon.CUT);
            Copy.Header = Resx.GetLabel(ResxCommon.COPY);
            Paste.Header = Resx.GetLabel(ResxCommon.PASTE);
        }

        /// <summary>
        /// Manages the context menu items state.
        /// </summary>
        public void ManageContextMenuItemState()
        {
            foreach (RadMenuItem l_oItem in ContextMenu.Items)
            {
                l_oItem.Visibility = CanExecute(l_oItem.Name) ? Visibility.Visible : Visibility.Collapsed;
                l_oItem.IsEnabled = l_oItem.Visibility != Visibility.Collapsed;
            }
        }

        /// <summary>
        /// Set user priviledges
        /// </summary>
        /// <param name="p_oFolderType"></param>
        /// <param name="p_oOrganization"></param>
        public void SetUserPriviledges(FolderType p_oFolderType, Organization p_oOrganization)
        {
            CanModify = GlobalWebContext.GetUserCanModify(p_oFolderType);
            CanDelete = GlobalWebContext.GetUserCanDelete(p_oFolderType);
            CanDownload = GlobalWebContext.GetUserCanDownload(p_oFolderType);
            CanPrint = GlobalWebContext.GetUserCanPrint(p_oFolderType);
            CanSendMail = GlobalWebContext.GetUserCanSendMail(p_oFolderType);
            CanImport = GlobalWebContext.GetUserCanImport(p_oOrganization, p_oFolderType);
            CanTrash = GlobalWebContext.GetUserCanTrash(p_oFolderType);
            ManageContextMenuItemState();
        }

        /// <summary>
        /// Notify for display mode changes
        /// </summary>
        /// <param name="p_oNewAppDisplayMode">new display mode</param>
        public void ChangeAppDisplayMode(AppDisplayMode p_oNewAppDisplayMode)
        {
            AppDisplayMode = p_oNewAppDisplayMode;
            ManageContextMenuItemState();
        }

        /// <summary>
        /// Set the IsEnabled property of a ribbon bar button.
        /// </summary>
        /// <param name="p_oItem">Button to</param>
        /// <param name="p_bIsEnabled">true or false</param>
        public void SetAvailability(DocumentRibbonBarMenuItems p_oItem, bool p_bIsEnabled, bool bypassUserRights = false)
        {
            RadMenuItem l_oRibbonButton = FindItemByName(p_oItem.ToString());
            if (l_oRibbonButton != null)
            {
                if (!bypassUserRights)
                {
                    if (m_oCanImport.Contains(p_oItem) && CanImport) l_oRibbonButton.Visibility = p_bIsEnabled ? Visibility.Visible : Visibility.Collapsed;
                    else if (m_oCanDelete.Contains(p_oItem) && CanDelete) l_oRibbonButton.Visibility = p_bIsEnabled ? Visibility.Visible : Visibility.Collapsed;
                    else if (m_oCanDownload.Contains(p_oItem) && CanDownload) l_oRibbonButton.Visibility = p_bIsEnabled ? Visibility.Visible : Visibility.Collapsed;
                    else if (m_oCanPrint.Contains(p_oItem) && CanPrint) l_oRibbonButton.Visibility = p_bIsEnabled ? Visibility.Visible : Visibility.Collapsed;
                    else if (m_oCanSendMail.Contains(p_oItem) && CanSendMail) l_oRibbonButton.Visibility = p_bIsEnabled ? Visibility.Visible : Visibility.Collapsed;
                    else l_oRibbonButton.Visibility = p_bIsEnabled ? Visibility.Visible : Visibility.Collapsed;
                }
                else
                {
                    l_oRibbonButton.Visibility = p_bIsEnabled ? Visibility.Visible : Visibility.Collapsed;
                }
            }
        }

        #endregion

        #region Private

        /// <summary>
        /// Finds the name of the item by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        RadMenuItem FindItemByName(string name)
        {
            foreach (var item in ContextMenu.Items)
            {
                if ((item as RadMenuItem).Name == name)
                {
                    return item as RadMenuItem;
                }
            }
            return null;
        }


        /// <summary>
        /// This is the common handler raised for every clicks in this ribbon bar
        /// IMPORTANT!! The name of the buttonSource in the XAML must be one of the DocumentRibbonBarMenuItems enumeration
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            DocumentRibbonBarMenuItems l_oDocumentRibbonBarMenuItem;
            string L_oContextMenuName = ((RadMenuItem)e.OriginalSource).Name;
            if (Enum.TryParse<DocumentRibbonBarMenuItems>(L_oContextMenuName, out l_oDocumentRibbonBarMenuItem))
            {
                this.DocumentContextMenuEvent(this,
                                            new DocumentRibbonBarEventArgs()
                                            {
                                                DocumentRibbonBarMenuItem = l_oDocumentRibbonBarMenuItem
                                            });

            }
            ManageContextMenuItemState();
        }
        #endregion

    }
}
